package com.fang.chain;

import com.fang.chain.config.LevelConfig;

import java.math.BigDecimal;

/**
 * @author fanglingxiao
 * @version 1.0
 * @description 责任链模式父类
 * 1.级别
 * 2.下一个处理者
 * 3.处理方式
 * @date 2022/4/24 1:37 下午
 **/
public abstract class AuditHandler {

    private AuditHandler nextHandler;
    private Integer level;

    public AuditHandler(AuditHandler nextHandler, Integer currLevel) {
        this.nextHandler = nextHandler;
        this.level = currLevel;
    }

    public final HandleResponse<String> executor(HandleRequest request) {
        if (level.equals(request.getHandleLevel())) {
            return this.handle(request);
        }
        if (this.nextHandler != null) {
            return this.nextHandler.handle(request);
        }
        return HandleResponse.fail();
    }

    /**
     * 0 - 100 客服直接审核
     * 101 - 500 需经理审核
     * 501 以上 需总监审核
     */
    public Integer calcLevel(BigDecimal price) {
        if (price == null) {
            return 0;
        }
        if (price.compareTo(new BigDecimal("100")) <= 0) {
            return LevelConfig.CUSTOMER_LEVEL;
        }
        if (price.compareTo(new BigDecimal("500")) <= 0) {
            return LevelConfig.MANAGER_LEVEL;
        }
        return LevelConfig.DIRECTOR_LEVEL;
    }

    /**
     *
     */
    protected abstract HandleResponse<String> handle(HandleRequest request);
}
